using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using EVELogApi;
using EVEChatLib;

namespace EVEChat
{
    public partial class ChatWindow : Form
    {
        // Create an instance of the log API
        API logApi = new API(Properties.Settings.Default.logPath);

        // Need the chatLib to!
        Lib chatLib = new Lib();

        // Okay now to start the timer class
        Timer ti = new Timer();

        // Some variables
        string lastChat = ""; // So we don't see the same chat twice
        string theChannelName; // The name to display in the title & request chat from the API

        // Array of Colors for the chat coloring
        Color[] chatAreaColors = new Color[4];

        public ChatWindow(string channelName)
        {
            InitializeComponent();

            // Set the title name
            this.Text = channelName.Replace('_', ' ');
            theChannelName = channelName;

            // Set colors
            chatAreaColors[0] = Properties.Settings.Default.nameColor;
            chatAreaColors[1] = Properties.Settings.Default.textColor;
            chatAreaColors[2] = Properties.Settings.Default.textColor2;
            chatAreaColors[3] = Properties.Settings.Default.backgroundColor;

            // Get the initial chat
            string[,] chat = logApi.getChat(theChannelName, "", false);

            lastChat = chatLib.addChatToRtb(chatArea, playerListBox, chat);

            ti.Interval = Properties.Settings.Default.chatRefresh;

            // Add the event handler
            this.ti.Tick += new System.EventHandler(this.tiTick);

            // Start it going!
            ti.Start();

            chatLib.addColorHeaders(chatArea, chatAreaColors);

        }

        public void tiTick(object sender, EventArgs e)
        {
            // Now get the new text!
            string[,] chat = logApi.getChat(theChannelName, lastChat, false);

            string temp = chatLib.addChatToRtb(chatArea, playerListBox, chat);

            if (temp != "")
            {
                lastChat = temp;
            }

            // See if the user has the chatbox selected
            if (chatArea.Focused == false)
            {
                // If they don't keep the text scrolled!
                chatLib.ScrollToEnd(chatArea);
            }
        }

        private void closing(object sender, FormClosingEventArgs e)
        {
            // Stop the timer
            ti.Stop();
        }

        private void bringToFront(object sender, EventArgs e)
        {
            this.BringToFront();
            this.Focus();
        }
    }
}